using System.Diagnostics;
using Godot;
using System;
using static InputConst;

namespace Views.Edit
{
    public class EditViewportInteractive : Control
    {
        #region Fields
        [Export]
        private StyleBox _selectStyle;
        private EditViewport _viewport;
        private EditView _view;
        private EditCanvas _canvas;
        private Button _zoomTitle;
        private bool _isCanvasMoveing;
        private bool _isSelecting;
        private Rect2 _selectRect;

        #endregion

        #region Propertys

        public EditView EditView
        {
            get
            {
                if (_view == null)
                    _view = EditViewport.EditView;
                return _view;
            }
        }

        public EditViewport EditViewport
        {
            get
            {
                if (_viewport == null)
                    _viewport = GetParent<EditViewport>();
                return _viewport;
            }
        }

        public EditCanvas EditCanvas
        {
            get
            {
                if (_canvas == null)
                    _canvas = EditViewport.EditCanvas;
                return _canvas;
            }
        }

        public Button ZoomTitleButton
        {

            get
            {
                if (_zoomTitle == null)
                    _zoomTitle = GetNode<Button>("Tools/ZoomTitle");
                return _zoomTitle;
            }
        }

        #endregion

        #region Private Functions

        private void OnZoomOut(bool isScreenChenter)
        {
            ZoomTitleButton.Text = ($"{EditCanvas.CanvasZoomOut(isScreenChenter):P1}");
        }

        private void OnZoomIn(bool isScreenChenter)
        {
            ZoomTitleButton.Text = ($"{EditCanvas.CanvasZoomIn(isScreenChenter):P1}");
        }

        private void OnZoomTitleUp()
        {
            EditCanvas.ResetScale();
            ZoomTitleButton.Text = "100.0%";
        }

        #endregion

        #region Godot API 

        public override void _Input(InputEvent @event)
        {
            if (GetGlobalRect().HasPoint(GetGlobalMousePosition()))
            {
                if (@event.IsActionReleased(CANVAS_ZOOM_IN))
                    OnZoomIn(false);
                else if (@event.IsActionReleased(CANVAS_ZOOM_OUT))
                    OnZoomOut(false);
                else if (@event.IsActionPressed(CANVAS_MOVE))
                    _isCanvasMoveing = EditCanvas.BeginCanvasMove(GetLocalMousePosition());
                else if (@event.IsActionReleased(CANVAS_MOVE))
                    _isCanvasMoveing = EditCanvas.EndCanvasMove();
                else if (_isCanvasMoveing && @event is InputEventMouseMotion e)
                    EditCanvas.CanvasMoveing(e.Position);
                else
                    return;
                AcceptEvent();
                Update();
            }
        }

        internal void UpdateSelect(bool isSelecting, Rect2 selectRect)
        {
            _isSelecting = isSelecting;
            _selectRect = selectRect;
            Update();
        }

        public override void _Draw()
        {
            base._Draw();
            if (_isSelecting)
            {
                var rect = new Rect2(_selectRect.Position * EditCanvas.Scale + EditCanvas.Position, _selectRect.Size * EditCanvas.Scale);
                DrawStyleBox(_selectStyle, rect);
            }

        }

        #endregion
    }
}